package com.mrjy.maintain.bo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.MessageInfo;
import com.mrjy.common.entity.QuestionComment;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.maintain.dao.IMessageInfoDao;
import com.mrjy.queries.dao.IQuestionCommentDao;

@Service("messageInfoBo")
public class MessageInfoBo implements IMessageInfoBo {

	@Resource
	private IMessageInfoDao messageInfoDao;
	@Resource
	private IQuestionCommentDao questionCommentDao;
	@Override
	public Object queryMsgList(UserInfo userInfo,MessageInfo messageInfo) {
		if (userInfo == null) {
			return "userInfo_error";
		}
		messageInfo.setUserId(userInfo.getId());
		if (messageInfo.getPage()>=1 && messageInfo.getRows()>=0) {
			messageInfo.setPageStart((messageInfo.getPage()-1)*messageInfo.getRows());
		}else {
			messageInfo.setPageStart(0);
			messageInfo.setRows(0);
		}
		if (messageInfo.getMsgModule() != null && messageInfo.getMsgModule().intValue()!=1 && messageInfo.getMsgModule().intValue()!=2) {
			messageInfo.setMsgModule(null);
		}
		List<MessageInfo> messageInfos = messageInfoDao.queryMsgList(messageInfo);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (MessageInfo m : messageInfos) {
			m.setSendTimeString(simpleDateFormat.format(m.getSendTime()));
		}
		return messageInfos;
	}

	@Override
	public Object readMsg(UserInfo userInfo,MessageInfo messageInfo) {
		if (userInfo == null) {
			return "userInfo_error";
		}
		messageInfo.setUserId(userInfo.getId());
		return messageInfoDao.selectByPrimaryKey(messageInfo);
	}

	@Override
	public Object updateMsgFlag(UserInfo userInfo, MessageInfo messageInfo) {
		if (userInfo == null) {
			return "userInfo_error";
		}
		if (messageInfo.getId()==null) {
			return null;
		}
		MessageInfo msg = new MessageInfo();
		msg.setId(messageInfo.getId());
		msg.setReadTag(1);
		msg.setUserId(userInfo.getId());
		int row = messageInfoDao.updateByPrimaryKeySelective(msg);
		if (row ==1) {
			return "ok";
		}
		return "error";
	}

	@Override
	public List<MessageInfo> queryListIsRead(UserInfo userInfo,
			MessageInfo messageInfo) {
		if (userInfo == null) {
			return new ArrayList<MessageInfo>();
		}
		if (messageInfo.getRows()>=0) {
			messageInfo.setPageStart(0);
		}else {
			messageInfo.setRows(0);
		}
		if (messageInfo.getId()==0) {
			messageInfo.setId(null);
		}
		messageInfo.setReadTag(1);
		messageInfo.setUserId(userInfo.getId());
		List<MessageInfo> messageInfos = messageInfoDao.selectListByPrimaryKey(messageInfo);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (MessageInfo m : messageInfos) {
			m.setSendTimeString(simpleDateFormat.format(m.getSendTime()));
		}
		return messageInfos;
	}

	@Override
	public List<MessageInfo> queryListNotRead(UserInfo userInfo,
			MessageInfo messageInfo) {
		if (userInfo == null) {
			return new ArrayList<MessageInfo>();
		}
		if (messageInfo.getRows()>=0) {
			messageInfo.setPageStart(0);
		}else {
			messageInfo.setRows(0);
		}
		if (messageInfo.getId()==0) {
			messageInfo.setId(null);
		}
		messageInfo.setReadTag(0);
		messageInfo.setUserId(userInfo.getId());
		List<MessageInfo> messageInfos = messageInfoDao.selectListByPrimaryKey(messageInfo);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (MessageInfo m : messageInfos) {
			m.setSendTimeString(simpleDateFormat.format(m.getSendTime()));
		}
		return messageInfos;
	}

	@Override
	public Object selectNotReadMsgCountByUserId(Integer userId) { 
		Map<String, Object> map = new HashMap<String, Object>();
		int count = messageInfoDao.selectNotReadMsgCountByUserId(userId);
		if (count > 0) {
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("total", count);
		}else {
			map.put("err_code", 0);
			map.put("err_msg", "没有数据");
		}
		return map;
	}

	@Override
	public Object queryPageMessageByUserId(MessageInfo info,
			Map<String, Object> map) {
		List<MessageInfo> list = messageInfoDao.queryPageMessageByUserId(info);
		int userId = info.getUserId();
		if(null != list && list.size() > 0){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("rows", list);
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "查询数据为空");
			map.put("rows", null);
		}
		return map;
	}
	@Override
	public Object queryMyPageMessageByUserId(MessageInfo info,
			Map<String, Object> map) {
		// TODO Auto-generated method stub
		Integer userId = info.getUserId();
		int count = messageInfoDao.selectNotReadMsgCountByUserId(userId);
        int totalUnReadCommentNum = questionCommentDao.queryTotalUnReadNumByCommentedId(info);
        int totalUnreadRecordNum = questionCommentDao.queryTotalUnreadRecordNumByCommentedId(userId);
        int totalUnReadNum = totalUnReadCommentNum + totalUnreadRecordNum;

		if(count > 0){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("count", count);
            map.put("totalUnReadNum", totalUnReadNum);
        }else{
			map.put("err_code", 1);
			map.put("err_msg", "查询数据为空");
			map.put("count", null);
		}
		return map;
	}

	@Override
	public Object queryMyMessageByUserId(MessageInfo info,
			Map<String, Object> map) {
		Integer userId = info.getUserId();
		
		int homeCount = 0;
		int consultantCount = 0;
		
		//顾问
		info.setText("payQA");
		int _totalUnreadNum = questionCommentDao.queryTotalUnReadNumByCommentedId(info);
		int _totalUnreadGoodNum = questionCommentDao.queryTotalUnreadGoodNumByCommentedId(info);
		int _totalUnreadRecordNum = questionCommentDao.queryTotalUnreadRecordNumByCommentedId(userId);
		consultantCount = _totalUnreadNum+_totalUnreadGoodNum+_totalUnreadRecordNum;
		 //家园
		info.setText("freeQA");
		int totalUnreadNum = questionCommentDao.queryTotalUnReadNumByCommentedId(info);
		int totalUnreadGoodNum = questionCommentDao.queryTotalUnreadGoodNumByCommentedId(info);
		//int totalUnreadRecordNum = questionCommentDao.queryTotalUnreadRecordNumByCommentedId(userId);
		homeCount = totalUnreadGoodNum+totalUnreadNum; //家园tab下的数字
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("homeCount",homeCount );
			map.put("consultantCount", consultantCount);
			//map.put("myCount", myCount);
		return map;
	}

	@Override
	public Object txUpdateMsgStatusBatch(QuestionComment comment, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
        UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
//		if(null == loginUser ){
//			map.put("err_code", -1);
//			map.put("err_msg", "用户未登录");
//			return map;
//		}
		if (comment.getText().equals("home")) {
			comment.setIsNeedPrivilege(0);
		}else if (comment.getText().equals("consultant")) {
			comment.setIsNeedPrivilege(1);
		}
		//批量已读 评论/回帖
		int row = messageInfoDao.updateCommentStatusBatch(comment);
		//批量已读 点赞
		int _row = messageInfoDao.updateGoodStatusBatch(comment);
		if (row>=0 && _row>=0) {
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
		}else {
			map.put("err_code", -1);
			map.put("err_msg", "更新失败");
		}
		return map;
	}
}
